package com.qupai.lib_base.widget.sticker

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Rect
import android.view.MotionEvent

class StickerActionIcon(private val context: Context) {
    // 资源缩放图片的位图
    private var srcIcon: Bitmap? = null
    private val rect: Rect = Rect()

    fun setSrcIcon(resource: Int) {
        srcIcon = BitmapFactory.decodeResource(context.resources, resource)
    }

    fun draw(canvas: Canvas, x: Float, y: Float) {
        // 画顶点缩放图片
        rect.left = (x - srcIcon!!.width / 2).toInt()
        rect.right = (x + srcIcon!!.width / 2).toInt()
        rect.top = (y - srcIcon!!.height / 2).toInt()
        rect.bottom = (y + srcIcon!!.height / 2).toInt()
        canvas.drawBitmap(srcIcon!!, null, rect, null)
    }

    /**
     * 判断手指触摸的区域是否在顶点的操作按钮内
     *
     * @param event
     * @return
     */
    fun isInActionCheck(event: MotionEvent): Boolean {
        val left = rect.left
        val right = rect.right
        val top = rect.top
        val bottom = rect.bottom
        return event.getX(0) >= left && event.getX(0) <= right && event.getY(0) >= top && event.getY(0) <= bottom
    }
}